package com.alibaba.citrus.cr.price.ability.functions.factory.price;

import com.alibaba.citrus.cr.price.ability.functions.entity.price.PriceDimensionRuleEntity;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crpricecenter.sdo.PriceDimensionSDO;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author: qiaojiange
 * @create: 2021/09/16
 * @Description 价格维度规则工厂
 */
public class PriceDimensionRuleFactory extends PriceBaseFactory {
    /**
     * 创建维度规则列表
     *
     * @param dimensionRuleIds
     * @return
     */
    public static List<PriceDimensionRuleEntity> batchCreate(List<String> dimensionRuleIds) {
        List<PriceDimensionRuleEntity> result = new ArrayList<>();
        Result<List<PriceDimensionSDO>> listResult = queryRepository.batchQueryDimensionRule(dimensionRuleIds);
        if (listResult.isSuccess() && CollectionUtils.isNotEmpty(listResult.getResult())) {
            listResult.getResult().forEach(e -> {
                PriceDimensionRuleEntity priceDimensionRuleEntity = PriceDimensionRuleEntity.of(e);
                result.add(priceDimensionRuleEntity);
            });
        }
        return result;
    }

    /**
     * 获取价格维度规则实体
     *
     * @param dimensionId 维度 ID
     * @return
     */
    public static PriceDimensionRuleEntity create(String dimensionId) {
        Result<PriceDimensionSDO> result = queryRepository.queryPriceDimension(dimensionId);
        if (result.isSuccess() && Objects.nonNull(result.getResult())) {
            return PriceDimensionRuleEntity.of(result.getResult());
        }
        return new PriceDimensionRuleEntity();
    }

    /**
     * 通过表名创建
     * @param tableName 表名
     * @return
     */
    public static PriceDimensionRuleEntity createByTableName(String tableName){
        Result<PriceDimensionSDO> result = queryRepository.queryPriceDimensionByTableName(tableName);
        if (result.isSuccess() && Objects.nonNull(result.getResult())) {
            return PriceDimensionRuleEntity.of(result.getResult());
        }
        return new PriceDimensionRuleEntity();
    }
}
